package com.example.springmvc.controller;

import com.example.springmvc.pojo.Item;
import com.example.springmvc.pojo.User;
import com.example.springmvc.service.IItemService;
import com.example.springmvc.service.IUserService;
import com.example.springmvc.vo.QueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
public class UserController {

    @Autowired
    private IUserService iUserService;
    @Autowired
    private IItemService itemService;

    /**
     * 查询所有用户
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/userList.action")
    public String selectAllUserList(HttpServletRequest request, ModelMap model) {

        List<User> userList=iUserService.queryUserList();

        // 把用户数据放在模型中
        model.addAttribute("userList", userList);

        return "userList";
    }


    //跳转修改用户页面
    // 4. 简单类型绑定参数
    // @RequestParam 指定绑定的参数名 默认值
    @RequestMapping("/userEdit.action")
    public String userEdit(@RequestParam(value = "id", required = true, defaultValue = "1") Integer id, Model model)
    {
        User user = iUserService.queryUserById(id);
        model.addAttribute("user",user);

        return "editUser";
    }

    //修改成功跳转信息页面
    @RequestMapping("/updateUser.action")
    public String updateUser(User user, MultipartFile pictureFile) throws IOException {

        // 图片上传
            // 设置图片名称，不能重复，可以使用uuid
            String picName = UUID.randomUUID().toString();
            // 获取文件名
            String oriName = pictureFile.getOriginalFilename();
            // 获取图片后缀
        if(!"".equals(oriName)){
            System.out.println(oriName);
            String extName = oriName.substring(oriName.lastIndexOf("."));
            // 开始上传
            pictureFile.transferTo(new File("D:/pic/" + picName + extName));
            // 设置图片名到用户中
            user.setPic(picName + extName);
        }
        // ---------------------------------------------
        // 更新
        iUserService.updateUserById(user);
        return "forward:/userList.action";//userEdit.action?id="+user.getId();
    }

    /**
     * 跳转到登录页面
     *
     * @return
     */
    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    /**
     * 用户登录
     *
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session) {
        // 校验用户登录
        /*System.out.println(username);
        System.out.println(password);*/
        // 把用户名放到session中
        session.setAttribute("username", username);
        session.setAttribute("password", password);
        return "redirect:/userList.action";
    }

    //根据商品名和用户名查询信息，同时接收维护人员多选数据
    //查询用户get请求方式
    @GetMapping("/queryUser2.action")
    public String queryUserGet(QueryVO queryVO,Model model) {
        String username = queryVO.getUser().getUsername();
        /*System.out.println(username);*/

        String itemName = queryVO.getItem().getName();
        /*System.out.println(itemName);*/

        Integer[] idArray = queryVO.getIds();
        /*System.out.println(Arrays.toString(idArray));*/

        if(idArray!=null){
            List<User> users = queryVO.getUserList();
            for(int i:idArray)
            {
                for (User j:users) {
                    if(i==j.getId()) {
                        System.out.println(j);
                    }
                }
            }
        }

        // 根据itemName查询商品数据
        List<Integer> ids = itemService.queryItemList2(itemName);
        //将用户名和商品集合存入map
        Map map=new HashMap();
        map.put("username",username);
        map.put("ids",ids);
        //根据商品名和用户名查询信息
        List<User> userList=iUserService.queryUserList2(map);
        // 把用户数据放在模型中
        model.addAttribute("userList", userList);
        return "userList";
    }

    //查询用户post请求方式
    @PostMapping("/queryUser2.action")
    public String queryUserPost(QueryVO queryVO,Model model) {
        String username = queryVO.getUser().getUsername();
        /*System.out.println(username);*/

        String itemName = queryVO.getItem().getName();
        /*System.out.println(itemName);*/

        Integer[] idArray = queryVO.getIds();
        /*System.out.println(Arrays.toString(idArray));*/

        if(idArray!=null){
            List<User> users = queryVO.getUserList();
            for(int i:idArray)
            {
                for (User j:users) {
                    if(i==j.getId()) {
                        System.out.println(j);
                    }
                }
            }
        }

        // 根据itemName查询商品数据
        List<Integer> ids = itemService.queryItemList2(itemName);
        //将用户名和商品名集合存入map
        Map map=new HashMap();
        map.put("username",username);
        map.put("ids",ids);
        //根据商品名和用户名查询信息
        List<User> userList=iUserService.queryUserList2(map);
        // 把用户数据放在模型中
        model.addAttribute("userList", userList);
        return "userList";
    }

    /**
     * 使用RESTful风格开发接口，实现根据id查询用户
     * @param id
     * @return
     */
    @GetMapping("user/{id}")
    @ResponseBody
    public User queryUserById1(@PathVariable() Integer id) {
        User user = this.iUserService.queryUserById(id);
        return user;
    }
    @GetMapping("user/")
    @ResponseBody
    public List<User> queryUserById2() {
        List<User> list = this.iUserService.queryUserList();
        return list;
    }
    @PostMapping("user/")
    @ResponseBody
    public User addUser(@RequestBody User user)
    {
        this.iUserService.addUser(user);
        return user;
    }
    @PutMapping("user/")
    @ResponseBody
    public User updateUser2(@RequestBody User user)
    {
        this.iUserService.updateUserById(user);
        return user;
    }

    @DeleteMapping("user/{id}")
    @ResponseBody
    public String deleteUserById(@PathVariable() Integer id)
    {
        this.iUserService.deleteById(id);
        return "ok";
    }

}
